import complex_query from '@dfeidao/atom-nodejs/db/complex-query';
import operate from '@dfeidao/atom-nodejs/db/operate';
import query from '@dfeidao/atom-nodejs/db/query';
import log from '@dfeidao/atom-nodejs/logger/log';
import { IncomingHttpHeaders } from 'http';
import { productid, systemid } from '../../atom/config';

interface Message {
	cookie: {
		uk: string;
		[key: string]: string
	};
	urls: {
		base: string;
		origin: string;
		url: string;
	};
	query: {};
	params: {};
	headers: {};
	captcha: string;
	data: {
		equip_no: string;
		equip_type_name: string;
		equip_type_no: string;
		equip_internal_no: string;
		equip_name: string;
		fixed_assets_type: string;
		equipment_state: string;
		transfer_time: string;
		transfer_price: string;
		transfer_date: number;
		// address: string;
	};
	hidden_info: Array<{ equip_no: string; hidden_source: string }>;
	hidden_data: Array<{
		hidden_source: string,
		hidden_name: string,
		assess_level: string
	}>;
	// design_data: Array<{
	// 	attr_no: string;
	// 	attr_name: string;
	// 	attr_value: string;
	// }>;
}

interface IWebResult {
	data: unknown;
	cookie?: {
		[name: string]: string;
	} | null;
	content_type?: string;
	headers?: {
		[key: string]: string;
	};
	attachment?: string;
	redirect?: string;
	status_code?: number;
}

export default async function atom(message: Message, action_id: string, session_id: string, headers: IncomingHttpHeaders): Promise<IWebResult> {
	log('Service begin path:e:\考核-2\kh\src\device-management-ygh\zj-000010\s006,action_id:' + action_id);
	const q = query(action_id, session_id, systemid);
	q.prepare('equip_hazard_source', ['_id'], { productid, equip_no: message.data.equip_no }, 200, 1, [], []);
	const [res] = await q.exec<{ _id: string }>();

	// const compx = complex_query(action_id, session_id, systemid);
	// const [sss] = await compx.add_field('equip_hazard_source', '_id', '_id')
	// 	.add_field('equ_user_defined_attribute', '_id', '_id')
	// 	.where_eq_tb('equip_hazard_source', 'equip_no', 'equ_user_defined_attribute', 'equip_no')
	// 	.exec<{ _id: string }>();
	const { exec, update, del, insert } = operate(action_id, session_id, systemid);
	update('equipment_facilities', { productid, equip_no: message.data.equip_no }, {
		equip_type_name: message.data.equip_type_name,
		equip_type_no: message.data.equip_type_no,
		equip_internal_no: message.data.equip_internal_no,
		equip_name: message.data.equip_name,
		fixed_assets_type: message.data.fixed_assets_type,
		equipment_state: message.data.equipment_state,
		transfer_time: message.data.transfer_time,
		transfer_price: message.data.transfer_price,
		transfer_date: message.data.transfer_date,
		productid
	});
	res.forEach((v) => {
		del('equip_hazard_source', { _id: v._id });
		del('equ_user_defined_attribute', { _id: v._id });
	});
	if (message.hidden_info && message.hidden_info.length > 0) {
		message.hidden_info.forEach((v) => {
			insert('equip_hazard_source', {
				equip_no: message.data.equip_no,
				hidden_source: v.hidden_source,
				productid
			});
		});
	}
	// const p = query(action_id, session_id, systemid);
	// p.prepare('equip_hazard_source', ['_id'], { productid, equip_no: message.data.equip_no }, 200, 1, [], []);
	// const [res1] = await q.exec<{ _id: string }>();
	// res1.forEach((v) => {
	// 	del('equip_hazard_source', { _id: v._id });
	// });
	// if (message.design_data && message.design_data.length > 0) {
	// 	message.design_data.forEach((j) => {
	// 		insert('equ_user_defined_attribute', {
	// 			attr_no: j.attr_no,
	// 			attr_name: j.attr_name,
	// 			attr_value: j.attr_value,
	// 			productid
	// 		});
	// 	});
	// }
	const t = await exec();
	log('Service end path:e:\考核-2\kh\src\device-management-ygh\zj-000010\s006,action_id:' + action_id);
	return {
		data: t
	};
}
